package com.example.nuc_ms_beta.ui.viewconstruction.mainfragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.nuc_ms_beta.R;
import com.example.nuc_ms_beta.ui.viewcomponent.slidmenuview.LeftScrolSlideMenu;
import com.example.nuc_ms_beta.ui.viewconstruction.newsfragment.LifeNewsFragment;
import com.example.nuc_ms_beta.ui.viewconstruction.newsfragment.OtherFragment;
import com.example.nuc_ms_beta.ui.viewconstruction.newsfragment.SchoolNewsFragment;

/**
 * Created by 张博川 on 2016/9/2.
 * 新闻 资讯 定
 */
public class NewsFragment extends Fragment implements View.OnClickListener {
    public static Context context;
    private View view;
    private LayoutInflater inflater;
    //用于 打开或者关闭设置
    private LeftScrolSlideMenu menu;
    //三个按钮 校内 校外 生活
    private Button bt_one;
    private Button bt_two;
    private Button bt_three;
    //打开设置按钮(本身是一个linearLayout)
    private ImageView newslayout_menu_iv;

    private Fragment tab_one;
    private Fragment tab_two;
    private Fragment tab_three;//
    private FrameLayout newslayout_framelayout;

    public NewsFragment(Context context, LeftScrolSlideMenu menu) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.menu = menu;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(container);
        initListener();
        SetFragment(1);
        return view;
    }

    private void initListener() {
        bt_one.setOnClickListener(this);
        bt_two.setOnClickListener(this);
        bt_three.setOnClickListener(this);
        newslayout_menu_iv.setOnClickListener(this);
    }

    private void initView(ViewGroup container) {
        this.view = inflater.inflate(R.layout.newslayout, container, false);
        bt_one = (Button) this.view.findViewById(R.id.newslayout_bt_one);
        bt_two = (Button) this.view.findViewById(R.id.newslayout_bt_two);
        bt_three = (Button) this.view.findViewById(R.id.newslayout_bt_three);
        newslayout_framelayout = (FrameLayout) this.view.findViewById(R.id.newslayout_framelayout);
        newslayout_menu_iv = (ImageView) this.view.findViewById(R.id.newslayout_menu_iv);
        //View image button

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.newslayout_bt_one:
                SetFragment(1);
                break;
            case R.id.newslayout_bt_two:
                SetFragment(2);
                break;
            case R.id.newslayout_bt_three:
                SetFragment(3);
                break;
            case R.id.newslayout_menu_iv:
                menu.OpenORCLose();
                break;
        }
    }

    private void SetFragment(int i) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        HideFragment(transaction);
        switch (i) {
            case 1:
                if (tab_one == null) {
                    tab_one = new SchoolNewsFragment(context);
                    transaction.add(R.id.newslayout_framelayout, tab_one);
                } else {
                    transaction.show(tab_one);
                }
                RemoveColor();
                RemoveDrawable();
                bt_one.setTextColor(Color.parseColor("#FFFFFF"));
                bt_one.setBackgroundResource(R.drawable.newslayout_topbutton);
                break;
            case 2:
                if (tab_two == null) {
                    tab_two = new OtherFragment(context);
                    transaction.add(R.id.newslayout_framelayout, tab_two);
                } else {
                    transaction.show(tab_two);
                }
                RemoveColor();
                RemoveDrawable();
                bt_two.setTextColor(Color.parseColor("#FFFFFF"));
                bt_two.setBackgroundResource(R.drawable.newslayout_topbutton);
                break;
            case 3:
                if (tab_three == null) {
                    tab_three = new LifeNewsFragment(context);
                    transaction.add(R.id.newslayout_framelayout, tab_three);
                } else {
                    transaction.show(tab_three);
                }
                RemoveColor();
                RemoveDrawable();
                bt_three.setTextColor(Color.parseColor("#FFFFFF"));
                bt_three.setBackgroundResource(R.drawable.newslayout_topbutton);
                break;
        }
        transaction.commit();
    }

    private void HideFragment(FragmentTransaction transaction) {
        if (tab_one != null) {
            transaction.hide(tab_one);
        }
        if (tab_two != null) {
            transaction.hide(tab_two);
        }
        if (tab_three != null) {
            transaction.hide(tab_three);
        }
    }

    private void RemoveDrawable() {
        bt_one.setBackgroundResource(R.drawable.newslayout_topbuttonother);
        bt_two.setBackgroundResource(R.drawable.newslayout_topbuttonother);
        bt_three.setBackgroundResource(R.drawable.newslayout_topbuttonother);
    }

    private void RemoveColor() {
        bt_one.setTextColor(Color.parseColor("#ADA79C"));
        bt_two.setTextColor(Color.parseColor("#ADA79C"));
        bt_three.setTextColor(Color.parseColor("#ADA79C"));
    }


}
